﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication1.用户控件
{
    public partial class GetFile : UserControl
    {
        static List<FileInformation> FileList = new List<FileInformation>();
        static List<FileInformation> DirectoryList = new List<FileInformation>();

        public GetFile()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();     //显示选择文件对话框
            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                this.textBox1.Text = openFileDialog1.FileName;          //显示文件路径
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                if (this.folderBrowserDialog1.SelectedPath.Trim() != "")
                    this.textBox1.Text = this.folderBrowserDialog1.SelectedPath.Trim();
            }
        }
        /// <summary>
        /// 打印目录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            DirectoryList.Clear();
            String path = @textBox1.Text;
            if (string.IsNullOrEmpty(path)) { return; }
            textBox4.Text = "";
            textBox3.Text = "";

            DirectoryInfo folder = new DirectoryInfo(path);

            GetAllDirectorys(folder,0);

            foreach (FileInformation file in DirectoryList)
            {
                //textBox4.Text += Path.GetFileNameWithoutExtension(file.FileName) + "\r\n";
                textBox4.Text += file.FilePath + "\r\n";
                textBox3.Text += Getkongge(file.shendu) + file.FileName + "\r\n";
            }
        }
        /// <summary>
        /// 打印所有文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            String path = @textBox1.Text;
            String houzhuei = textBox2.Text;
            if (string.IsNullOrEmpty(path)) { return; }
            FileList.Clear();
            textBox4.Text = "";
            textBox3.Text = "";

            DirectoryInfo folder = new DirectoryInfo(path);

            GetAllFiles(folder, houzhuei);

            foreach (FileInformation file in FileList)
            {
                //textBox4.Text += Path.GetFileNameWithoutExtension(file.FileName) + "\r\n";
                textBox4.Text += file.FilePath + "\r\n";
                textBox3.Text += file.FileName + "\r\n";
            }
            button5.Enabled = true;
        }

        public string Getkongge(int shendu) {
            string returnstr = "";
            for (int i = 0; i < shendu; i++)
            {
                returnstr += "   ";
            }
            return returnstr;
        }

        public static List<FileInformation> GetAllDirectorys(DirectoryInfo dir,int shendu)
        {
            DirectoryList.Add(new FileInformation { FileName = dir.Name, FilePath = dir.FullName,shendu = shendu });
            DirectoryInfo[] allDir = dir.GetDirectories();
            foreach (DirectoryInfo d in allDir)
            {
                GetAllDirectorys(d, shendu+1);
            }
            return FileList;
        }

        public static List<FileInformation> GetAllFiles(DirectoryInfo dir,string houzhuei)
        {
            FileInfo[] allFile = dir.GetFiles(houzhuei);
            foreach (FileInfo fi in allFile)
            {
                FileList.Add(new FileInformation { FileName = fi.Name, FilePath = fi.FullName, allFile = fi });
            }
            DirectoryInfo[] allDir = dir.GetDirectories();
            foreach (DirectoryInfo d in allDir)
            {
                GetAllFiles(d, houzhuei);
            }
            return FileList;
        }

        private void button5_Click(object sender, EventArgs e)
        {

            String path = @textBox1.Text;
            String Newpath = @textBox5.Text;
            if (string.IsNullOrEmpty(path) || string.IsNullOrEmpty(Newpath)) { MessageBox.Show("填入信息不完整！"); return; }
            textBox4.Text = "";
            textBox3.Text = "";

            foreach (FileInformation file in FileList)
            {
                string wenjianming = Utils.GetGUID() + file.allFile.Extension;
                string destPath = Path.Combine(Newpath, wenjianming);
                //if (item.Name.Length > 10)
                //{
                //    //判断是否重名
                //    if (!File.Exists(destPath))
                //    {
                file.allFile.CopyTo(destPath);
                textBox3.Text += file.allFile.Name + "\r\n";
                textBox4.Text += wenjianming+"\r\n";
                //    }
                //    i++;
                //}
            }

            //DirectoryInfo directoryinfo = new DirectoryInfo(path);
            //int i = 1;
            ////读取当前目录文件信息
            //foreach (var item in directoryinfo.GetFiles())
            //{

            //    string destPath = Path.Combine(Newpath, i + item.Extension);
            //    if (item.Name.Length > 10)
            //    {
            //        //判断是否重名
            //        if (!File.Exists(destPath))
            //        {
            //            item.MoveTo(destPath);
            //        }
            //        i++;
            //    }
            //}
            MessageBox.Show("操作完成");
        }

        private void textBox5_Click(object sender, EventArgs e)
        {
            DialogResult dr = folderBrowserDialog1.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                textBox5.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        private void textBox3_DoubleClick(object sender, EventArgs e)
        {
            textBox3.Focus();
            textBox3.SelectAll();
        }

        private void textBox4_DoubleClick(object sender, EventArgs e)
        {
            textBox4.Focus();
            textBox4.SelectAll();
        }

        private void button6_Click(object sender, EventArgs e)
        {

            MessageBoxButtons messButton = MessageBoxButtons.OKCancel;
            DialogResult dr = MessageBox.Show("是否在原目录重命名？", "提示", messButton);

            if (dr == DialogResult.OK)//如果点击“确定”按钮
            {

                String path = @textBox1.Text;
                if (string.IsNullOrEmpty(path)) { MessageBox.Show("填入信息不完整！"); return; }

                var val1 = textBox3.Text;
                var val2 = textBox4.Text;

                string[] list2 = val2.Split(Environment.NewLine.ToCharArray());
                List<string> newlist = new List<string>();

                foreach (var item in list2)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        newlist.Add(item);
                    }
                }

                if (FileList.Count() != newlist.Count())
                {
                    MessageBox.Show("两边数量不对等！"); return;
                }

                int i = 0;
                foreach (FileInformation file in FileList)
                {

                    string wenjianming = newlist[i];// + file.allFile.Extension;
                    
                    string destPath = Path.Combine(Path.GetDirectoryName(file.FilePath), wenjianming);

                    file.allFile.MoveTo(destPath);

                    i++;
                }
                MessageBox.Show("操作完成");

            }
            if (dr == DialogResult.Cancel)//如果点击“取消”按钮
            {
                String path = @textBox1.Text;
                String Newpath = @textBox5.Text;
                if (string.IsNullOrEmpty(path) || string.IsNullOrEmpty(Newpath)) { MessageBox.Show("填入信息不完整！"); return; }

                var val1 = textBox3.Text;
                var val2 = textBox4.Text;

                string[] list2 = val2.Split(Environment.NewLine.ToCharArray());
                List<string> newlist = new List<string>();

                foreach (var item in list2)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        newlist.Add(item);
                    }
                }

                if (FileList.Count() != newlist.Count())
                {
                    MessageBox.Show("两边数量不对等！"); return;
                }

                int i = 0;
                foreach (FileInformation file in FileList)
                {

                    string wenjianming = newlist[i];// + file.allFile.Extension;
                    string destPath = Path.Combine(Newpath, wenjianming);

                    file.allFile.CopyTo(destPath);

                    i++;
                }
                MessageBox.Show("操作完成");
            }

            
        }
        
    }

    public class FileInformation
    {
        public string FileName { get; set; }
        public string FilePath { get; set; }
        public int shendu { get; set; }
        public FileInfo allFile { get; set; }
    }

}
